﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Submarines
{
    public class Remote
    {
        public bool _prev_hited = false;
        public bool _random_hited = false;
        public int _vector = 0;
        //int _one_take_count = 0;
        //public bool _continue_if_missed = false;
        public List<int> _vectors = new List<int>();
        static Random _rand1 = new Random();
        public TableLayoutPanel _remoteTable = new TableLayoutPanel();
        //TableLayoutPanelCellPosition _cellToBeHit = new TableLayoutPanelCellPosition();
        TableLayoutPanelCellPosition _sourceCell = new TableLayoutPanelCellPosition();
        //TableLayoutPanelCellPosition _hittedCell = new TableLayoutPanelCellPosition();

        public Remote(TableLayoutPanel tblPanel)
        {
            _remoteTable = tblPanel;
            foreach (Control c in _remoteTable.Controls)
            {
                Button b = c as Button;
                if (b != null)
                    b.Tag = "0";
            }
            FillVectors();
        }

        public void FillVectors()
        {
            _vectors.Clear();
            _vectors.Add(1);
            _vectors.Add(2);
            _vectors.Add(3);
            _vectors.Add(4);
        }
        public TableLayoutPanelCellPosition GetRabdomCell()
        {
            Button btn= new Button();
            btn.Tag = "4";
                while (!btn.Tag.ToString().Equals("1") && !btn.Tag.ToString().Equals("2"))
                {
                    _sourceCell.Column = GetRandomNumber(0, 99);
                    _sourceCell.Row = GetRandomNumber(0, 99);
                    btn = (Button)_remoteTable.GetControlFromPosition(_sourceCell.Column, _sourceCell.Row);
                }
            return _sourceCell;
        }
        //___________________________________________________________________________________
        //TableLayoutPanelCellPosition CheckNextMove()
        //{
        //    bool cell_ok = false;
        //    TableLayoutPanelCellPosition tempCell = new TableLayoutPanelCellPosition();
        //    if (_vector == 0)
        //    {
        //        _illigal_vectors.Clear();
        //        _illigal_vectors.Add(0);
        //    }
        //    while (!cell_ok)
        //    {
        //        if (_illigal_vectors.Count == 4)
        //        {
        //            _continue_if_missed = false;
        //            _illigal_vectors.Clear();
        //        }
        //        //else
        //        //{
        //        //    _continue_if_missed = true;
        //        //}
        //        //if(!_prev_hited)
        //        //    _vector = GetDirection(_illigal_vectors);
        //        tempCell = GetCellByVector(_vector);
        //        cell_ok = CheckCellToHit(tempCell);
        //        _illigal_vectors.Add(_vector); // ??????????????????????????????????
        //    }

        //    return tempCell;
        //}

        public bool CheckCellToHit(TableLayoutPanelCellPosition tempCell)
        {
            // 0-out of range
            // 1-ok to hit
            // 2-allready missd

            Button btn;
            try
            {
                btn = (Button)_remoteTable.GetControlFromPosition(tempCell.Column, tempCell.Row);
            }
            catch (ArgumentException)
            {
                return false;
            }
            if (btn == null)
                return false;
            //if (btn.Tag.ToString().Equals("1"))
            if (!btn.Tag.ToString().Equals("3") && !btn.Tag.ToString().Equals("4"))
                return true;
            else
                return false;
        }

        public TableLayoutPanelCellPosition GetNexCellByHitted(TableLayoutPanelCellPosition cell, bool with_vector)
        {
            bool ok = false;
            TableLayoutPanelCellPosition tempCell = new TableLayoutPanelCellPosition();
            while (!ok && _vectors.Count > 0)
            {
                if (with_vector)
                    SetDirection();
                switch (_vector)
                {
                    case 1:
                        tempCell.Column = cell.Column - 1;
                        tempCell.Row = cell.Row;
                        break;
                    case 2:
                        tempCell.Column = cell.Column;
                        tempCell.Row = cell.Row - 1;
                        break;
                    case 3:
                        tempCell.Column = cell.Column + 1;
                        tempCell.Row = cell.Row;
                        break;
                    case 4:
                        tempCell.Column = cell.Column;
                        tempCell.Row = cell.Row + 1;
                        break;
                }
                if (with_vector)
                    ok = CheckCellToHit(tempCell);
                else
                    ok = true;
            }
            if (_vectors.Count == 0)
            {
                //tempCell.Column = -3;
                _prev_hited = false;
            }
            return tempCell;
        }
        public Button GetButtonByCell(TableLayoutPanelCellPosition cell)
        {
            return _remoteTable.GetControlFromPosition(cell.Column, cell.Row) as Button;
        }
        public void SetDirection()
        {
            _vector = 0;
            while (!_vectors.Exists (delegate (int x){return x == _vector;}) || _vector ==0)
                _vector = GetRandomVector();
            _vectors.Remove(_vector);
        }

        public void MarkCellsWhenHitted(TableLayoutPanelCellPosition cell)
        {
            try { ((Button)_remoteTable.GetControlFromPosition(cell.Column - 1, cell.Row - 1)).Tag = "4"; }
            catch (Exception) { }
            try { ((Button)_remoteTable.GetControlFromPosition(cell.Column - 1, cell.Row + 1)).Tag = "4"; }
            catch (Exception) { }
            try { ((Button)_remoteTable.GetControlFromPosition(cell.Column + 1, cell.Row - 1)).Tag = "4"; }
            catch (Exception) { }
            try { ((Button)_remoteTable.GetControlFromPosition(cell.Column + 1, cell.Row + 1)).Tag = "4"; }
            catch (Exception) { }
            ((Button)_remoteTable.GetControlFromPosition(cell.Column, cell.Row)).Tag = "1";
        }
        public void MarkMissed(TableLayoutPanelCellPosition missed_cell)
        {
            ((Button)_remoteTable.GetControlFromPosition(missed_cell.Column, missed_cell.Row)).Tag = "4";
        }
        int GetRandomNumber(int min, int max)
        {
            return _rand1.Next(min, max) % 10;
        }
        int GetRandomVector()
        {
            int temp = _rand1.Next(1, 100);
            return temp % 5;
        }


    }
}
